package com.claimSettlement.service.impl;

import com.claimSettlement.mapper.WarrantyMapper;
import com.claimSettlement.pojo.Warranty;
import com.claimSettlement.service.WarrantyService;
import com.claimSettlement.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保单Service实现类
 */
@Service
public class WarrantyServiceImpl implements WarrantyService {
    @Resource
    WarrantyMapper warrantyMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    public List<Warranty> getWarrantyAll(){
        redisTemplate.delete("warrantyList");
        List<Warranty> warrantyList=null;
        if(redisTemplate.hasKey("warrantyList")){
            warrantyList= JsonUtils.jsonToList(redisTemplate.opsForHash().values("warrantyList").toString(),Warranty.class);
        }else{
            warrantyList=warrantyMapper.getWarrantyAll();
            Map<String,Object> map=new HashMap<String, Object>();
            for (Warranty warrantys:warrantyList
                 ) {
                map.put(warrantys.getWarrantyId(),JsonUtils.objectToJson(warrantys));
            }
            redisTemplate.opsForHash().putAll("warrantyList",map);
        }
        return warrantyList;
    }

    public int updateWarranty(Warranty warranty){
        int count=warrantyMapper.updateWarranty(warranty);
        if(count>0){
            redisTemplate.opsForHash().put("warrantyList",warranty.getWarrantyId(),JsonUtils.objectToJson(warranty));
        }
        return count;
    }

    public int updWarrantyState(String warrantyState, String warrantyId) {
        int count=warrantyMapper.updWarrantyState(warrantyState, warrantyId);
        if(count>0){
            Warranty warranty=warrantyMapper.getWarrantyOne(warrantyId);
            redisTemplate.opsForHash().put("warrantyList",warrantyId,JsonUtils.objectToJson(warranty));
        }
        return count;
    }

    public int addWarranty(Warranty warranty) {
        warrantyMapper.updateNum2();
        warranty.setWarrantyId(warrantyMapper.getLius());
        int count=warrantyMapper.addWarranty(warranty);
        if(count>0){
            redisTemplate.opsForHash().put("warrantyList",warranty.getWarrantyId(),JsonUtils.objectToJson(warranty));
        }
        return count;
    }

    public Warranty getWarrantyOne(String warrantyId) {
        return warrantyMapper.getWarrantyOne(warrantyId);
    }

    public List<Warranty> getWarrantyId(String warrantyId, String recognizeeName, String plateNumber, String frameId) {
        return warrantyMapper.getWarrantyId(warrantyId, recognizeeName, plateNumber, frameId);
    }

    public String getWarrantyNow() {
        return warrantyMapper.getWarrantyNow();
    }

    public List<Integer> selectRisk(String warrantyId) {
        return warrantyMapper.selectRisk(warrantyId);
    }

    public int insertInsure(String warrantyId, int riskTypeId) {
        return warrantyMapper.insertInsure(warrantyId, riskTypeId);
    }

    public int updatePremiumTotal(double premiumTotal, String warrantyId) {
        int count=warrantyMapper.updatePremiumTotal(premiumTotal, warrantyId);
        if(count>0){
            Warranty warranty=warrantyMapper.getWarrantyOne(warrantyId);
            redisTemplate.opsForHash().put("warrantyList",warrantyId,JsonUtils.objectToJson(warranty));
        }
        return count;
    }

    public List<String> getCaseChepai(String caseId) {
        return warrantyMapper.getCaseChepai(caseId);
    }

    public int getWarrantyCount(String recognizeeId,String plateNumber) {
        return warrantyMapper.getWarrantyCount(recognizeeId,plateNumber);
    }

    public List<Warranty> getWarrantyAll2() {
        return warrantyMapper.getWarrantyAll2();
    }
}
